package com.match.competition_apply.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.match.commonutils.Result;
import com.match.competition_apply.entity.EntryLevel;
import com.match.competition_apply.service.EntryLevelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author AdiosF
 * @since 2021-09-22
 */
@Api(tags = "参赛级别模块")
@RestController
@RequestMapping("/competition_apply/entry-level")
public class EntryLevelController {
    @Autowired
    private EntryLevelService entryLevelService;

    @ApiOperation("添加级别")
    @PostMapping("/addEntryLevel")
    public Result addEntryLever(@ApiParam(readOnly = true,value = "参赛级别")
                           @RequestBody EntryLevel entryLevel){
        QueryWrapper<EntryLevel> entryLevelQueryWrapper=new QueryWrapper<>();
        String title = entryLevel.getTitle();
        if (!StringUtils.isEmpty(title)){
            entryLevelQueryWrapper.eq("title",title);
        }
        int count = entryLevelService.count(entryLevelQueryWrapper);
        if (count>0){
            return Result.error().message("请勿重复添加参赛级别");
        }
        boolean save = entryLevelService.save(entryLevel);
        if (!save){
            return Result.error().message("参赛级别添加失败");
        }
        return Result.ok().message("参赛级别添加成功");
    }


    @ApiOperation("查询所有的参赛级别")
    @GetMapping("/findAllEntryLever")
    public Result findAllEntryLever(){
        List<EntryLevel> list = entryLevelService.list(null);
        if (list.isEmpty()){
            return Result.error().message("暂无参赛级别请添加后查询");
        }
        return  Result.ok().data("EntryLeverList",list);
    }
}

